﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace kostenbuch
{
    class DrawChart
    {
       double ang =-0.1;
       int count = 0;
       static Double radius = 100.0;
       Point sPoint = new Point(0, 0);
       bool first = true;
       public DrawChart()
       {           
       }
       /*
        * @Parameter  Winkel in Prozent, Canavas
        * Zeichnet eine Kreisstück mit vorgegebenem Radius und dem übergebenem Winkel
        * Für die ersten Aufruffe werden neue Farben zugeordnet, dann folgt eine default Farbe.
        */        
        public Path drawPie(double precent, Canvas c)
        {
            ang += precent;
            Path path = new Path();
            Canvas.SetLeft(path, c.ActualWidth / 2);
            Canvas.SetTop(path, c.ActualHeight / 2);
            #region Switch-Case-Farben
            switch (count)
            {
                case 0:
                    {
                        path.Fill = Brushes.Red;
                        path.Stroke = Brushes.Red;
                        count++;
                        break;
                    }
                case 1:
                    {
                        path.Fill = Brushes.Blue;
                        path.Stroke = Brushes.Blue;
                        count++;
                        break;
                    }
                case 2:
                    {
                        path.Fill = Brushes.Orange;
                        path.Stroke = Brushes.Orange;
                        count++;
                        break;
                    }
                case 3:
                    {
                        path.Fill = Brushes.Indigo;
                        path.Stroke = Brushes.Indigo;
                        count++;
                        break;
                    }
                case 4:
                    {
                        path.Fill = Brushes.Fuchsia;
                        path.Stroke = Brushes.Fuchsia;
                        count++;
                        break;
                    }
                case 5:
                    {
                        path.Fill = Brushes.DeepSkyBlue;
                        path.Stroke = Brushes.DeepSkyBlue;
                        count++;
                        break;
                    }
                case 6:
                    {
                        path.Fill = Brushes.DeepPink;
                        path.Stroke = Brushes.DeepPink;
                        count =  1;
                        break;
                    }
                case -1:
                    {
                        path.Fill = Brushes.Gray;
                        path.Stroke = Brushes.Gray;
                        count++;
                        break;
                    }
                default:
                    {
                        path.Fill = Brushes.Green;
                        path.Stroke = Brushes.Green;
                        break;
                    }
            }
            #endregion
            PathGeometry pathGeometry = new PathGeometry();
            PathFigure pathFigure = new PathFigure();           
            pathFigure.StartPoint = new Point(0,0);            
            pathFigure.IsClosed = true;            
            Double angle = (360.0 / 100.0) * ang;
            // Starting Point
            LineSegment lineSegment;
            if (first)
            {
                lineSegment = new LineSegment(new Point(radius, 0), false);
                first = false;
            }
            else
            {
                 lineSegment = new LineSegment(sPoint, false);
            }
            // Arc
            ArcSegment arcSegment = new ArcSegment();
            arcSegment.IsLargeArc = precent >= 50;
            arcSegment.Point = new Point(Math.Cos(angle * Math.PI / 180) * radius, Math.Sin(angle * Math.PI / 180) * radius);
            sPoint = arcSegment.Point;
            arcSegment.Size = new Size(radius, radius);
            arcSegment.SweepDirection = SweepDirection.Clockwise;
            pathFigure.Segments.Add(lineSegment);
            pathFigure.Segments.Add(arcSegment);
            pathGeometry.Figures.Add(pathFigure);
            path.Data = pathGeometry;
            return path;
        }
        /*
         *@Parameters: Höhe des Balken in Prozent, Canvas
         *Zeichnet ein Balkendiagramm. Beim Zweiten Aufruf wird der Balken horizontal versetzt und die Farbe geändert.
         */
        public Path drawBar(double precent, Canvas c)
        {
            precent = c.Height - ((c.Height / 100) * precent);
            Path path = new Path();
            PathGeometry pathGeometry = new PathGeometry();
            PathFigure pathFigure = new PathFigure();
            pathFigure.StartPoint = new Point(0, c.Height);
            pathFigure.IsClosed = true;
            LineSegment lS1, lS2, lS3, lSTrans1;        
            if (first)
            {
                pathFigure.StartPoint = new Point(0, c.Height);
                path.Fill = Brushes.DarkRed;
                path.Stroke = Brushes.DarkRed;
                lS1 = new LineSegment(new Point(0, precent), true);
                lS2 = new LineSegment(new Point(40, precent), true);
                lS3 = new LineSegment(new Point(40, c.Height), true);                
                first = false;
            }
            else
            {
                pathFigure.StartPoint = new Point(60, c.Height);
                path.Fill = Brushes.Green;
                path.Stroke = Brushes.Green;
                lS1 = new LineSegment(new Point(60, precent), true);
                lS2 = new LineSegment(new Point(100, precent), true);
                lS3 = new LineSegment(new Point(100,c.Height), true);               
            }  
            lSTrans1 = new LineSegment(new Point(0, c.Height), true);           
            pathFigure.Segments.Add(lS1);
            pathFigure.Segments.Add(lS2);
            pathFigure.Segments.Add(lS3);
            pathFigure.Segments.Add(lSTrans1);
            pathGeometry.Figures.Add(pathFigure);
            path.Data = pathGeometry;
            return path;
        }
        /*
         * @Parameters: prozent, canvas
         * Setzt den count für die switch case Abfrage auf -1 um grau zu zeichnen.
         * Ruft dann die drawPie Methode auf.
         */
        public Path drawPieMisc(double less, Canvas c)
        {
            count = -1;
            return drawPie(less, c);

        }
    }
}
